﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ET.DBCommon.DAL;
using ET.DBCommon.Model;

namespace ET.ScoreTeacherClient.Views
{
    /// <summary>
    /// UserManager.xaml 的交互逻辑
    /// </summary>
    public partial class UserManager : UserControl, INotifyPropertyChanged
    {
        public UserManager()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #region UserList
        private ObservableCollection<UserViewModel> m_UserList = new ObservableCollection<UserViewModel>();

        public ObservableCollection<UserViewModel> UserList
        {
            get
            {
                return this.m_UserList;
            }
            set
            {
                this.m_UserList = value;
                this.OnPropertyChanged();
            }
        }
        #endregion

        #region SelectedUser
        private UserViewModel m_SelectedUser;

        public UserViewModel SelectedUser
        {
            get
            {
                return this.m_SelectedUser;
            }
            set
            {
                this.m_SelectedUser = value;
                this.OnPropertyChanged();
            }
        }
        #endregion

        #region INotifyPropertyChanged 接口实现
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string propName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
        #endregion
        private void BtnTiming_Click(object sender, RoutedEventArgs e)
        {
            Button button = e.OriginalSource as Button;
            if (this.SelectedUser == null && button.Content.ToString() != "新建")
            {
                MessageBox.Show("请选择用户!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            switch (button.Content.ToString())
            {
                case "新建":
                    UserViewModel viewModel = new UserViewModel();
                    UserEdit userEdit = new UserEdit(viewModel);
                    if (userEdit.ShowDialog() == true)
                    {
                        this.UserList.Add(viewModel);
                    }
                    break;
                case "修改":
                    viewModel = this.SelectedUser.DeepClone();
                    userEdit = new UserEdit(viewModel);
                    if (userEdit.ShowDialog() == true)
                    {
                        viewModel.Copy(this.SelectedUser);
                    }
                    break;
                case "删除":
                    if (UserInfoDAL.DeleteUser(this.SelectedUser.Id))
                    {
                        this.UserList.Remove(this.SelectedUser);
                    }
                    break;
                case "人脸注册":
                    new FaceRegister(this.SelectedUser).ShowDialog();
                    break;
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.UserList.Clear();
            List<UserInfoModel> userList = UserInfoDAL.SelectUserList();
            userList.ForEach(p => this.UserList.Add(new UserViewModel(p)));
        }
    }

    public class UserViewModel : NotifyPropertyChanged
    {
        public int Id { get; set; }

        private string m_UserNumber;

        public string UserNumber
        {
            get
            {
                return this.m_UserNumber;
            }
            set
            {
                this.m_UserNumber = value;
                this.OnPropertyChanged();
            }
        }

        private string m_UserName;

        public string UserName
        {
            get
            {
                return this.m_UserName;
            }
            set
            {
                this.m_UserName = value;
                this.OnPropertyChanged();
            }
        }

        private string m_Gender;

        public string Gender
        {
            get
            {
                return this.m_Gender;
            }
            set
            {
                this.m_Gender = value;
                this.OnPropertyChanged();
            }
        }

        private string m_Age;

        public string Age
        {
            get
            {
                return this.m_Age;
            }
            set
            {
                this.m_Age = value;
                this.OnPropertyChanged();
            }
        }
        private string m_MobilePhone;

        public string MobilePhone
        {
            get
            {
                return this.m_MobilePhone;
            }
            set
            {
                this.m_MobilePhone = value;
                this.OnPropertyChanged();
            }
        }
        private string m_IdCard;

        public string IdCard
        {
            get
            {
                return this.m_IdCard;
            }
            set
            {
                this.m_IdCard = value;
                this.OnPropertyChanged();
            }
        }
        private string m_Group;

        public string Group
        {
            get
            {
                return this.m_Group;
            }
            set
            {
                this.m_Group = value;
                this.OnPropertyChanged();
            }
        }

        public string Photo { get; set; }

        public int ATSUserID { get; set; }

        private string m_FaceRegister;

        public string FaceRegister
        {
            get
            {
                return this.m_FaceRegister;
            }
            set
            {
                this.m_FaceRegister = value;
                this.OnPropertyChanged();
            }
        }

        public UserViewModel()
        {

        }

        public UserViewModel(UserInfoModel model)
        {
            this.Id = model.Id;
            this.UserName = model.UserName;
            this.UserNumber = model.UserNumber;
            this.Gender = model.Gender;
            this.Age = model.Age;
            this.MobilePhone = model.MobilePhone;
            this.IdCard = model.IdCard;
            this.Group = model.Group;
        }

        public UserInfoModel Model
        {
            get
            {
                return new UserInfoModel()
                {
                    Id = this.Id,
                    UserName = this.UserName,
                    UserNumber = this.UserNumber,
                    Gender = this.Gender,
                    Age = this.Age,
                    MobilePhone = this.MobilePhone,
                    IdCard = this.IdCard,
                    Group = this.Group,
                };
            }
        }

        public UserViewModel DeepClone()
        {
            return new UserViewModel()
            {
                Id = this.Id,
                UserName = this.UserName,
                UserNumber = this.UserNumber,
                Gender = this.Gender,
                Age = this.Age,
                MobilePhone = this.MobilePhone,
                IdCard = this.IdCard,
                Group = this.Group,
            };
        }

        public void Copy(UserViewModel viewModel)
        {
            viewModel.Id = this.Id;
            viewModel.UserName = this.UserName;
            viewModel.UserNumber = this.UserNumber;
            viewModel.Gender = this.Gender;
            viewModel.Age = this.Age;
            viewModel.MobilePhone = this.MobilePhone;
            viewModel.IdCard = this.IdCard;
            viewModel.Group = this.Group;
        }
    }
}
